/*
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim </a>, 2004
 */

package hk.com.carfield.item;

import hk.com.carfield.content.Content;
import hk.com.carfield.content.Redirector;
import hk.com.carfield.core.ContentManager;
import hk.com.carfield.core.Context;
import hk.com.carfield.core.ItemManager;
import hk.com.carfield.util.FileUtil;
import hk.com.carfield.util.UrlUtil;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.SortedSet;

import org.apache.commons.lang.StringUtils;

public abstract class FileItem implements Item {
	public static class Default extends FileItem {
		public Default(final Context c, final File f, final boolean initPrevNext) throws IOException {
			super(c, f, initPrevNext);
		}

		public final String getDesc() throws IOException {
			return standardDesc();
		}

		public String getFolder() throws IOException {
			return UrlUtil.getUrl(context.config, file.getParentFile());
		}
	}

	final Context context;

	final Content content;

	final File file, prev, next;

	public static final FileItem getFileItem(final Context c, final File f, final boolean initPrevNext)
			throws IOException {
		return new Default(c, f, initPrevNext);
	}

	private static int maxDescLength = 500;

	protected final String standardDesc() throws IOException {
		String desc = context.persistMan.getFullDesc(context, file);
		if (!desc.contains("<") && !desc.contains(">")) {
			// HTML
			if (desc.length() >= maxDescLength) {
				desc = StringUtils.abbreviate(desc, maxDescLength);
			}
		}

		return desc;
	}

	public FileItem(final Context c, final File f, final boolean initPrevNext) throws IOException {
		file = f;
		context = c;
		if (initPrevNext) {
			int index = -1;
			final FileItem[] list = context.cacheMan.getSortedPeerFileItems(context, f.getParentFile(), ItemManager
					.getComparator(null));
			for (int i = 0; i < list.length; i++) {
				if (list[i].file.getAbsolutePath().equalsIgnoreCase(file.getAbsolutePath())) {
					index = i;
					break;
				}
			}
			prev = index > 0 ? list[index - 1].file : null;
			next = index >= 0 && index < list.length - 1 ? list[index + 1].file : null;
		} else {
			prev = null;
			next = null;
		}
		content = ContentManager.getContent(context, file);
	}

	public final String getUrl() throws IOException {
		return content instanceof Redirector ? ((Redirector) content).getUrl() : UrlUtil.getWrappedUrl(context.config,
				file);
	}

	public final String getName() {
		final String fileName = file.getName();
		return fileName;
	}

	public final Date getDate() {
		return new Date(file.lastModified());
	}

	public final Item getNext() throws IOException {
		return next == null ? null : ItemManager.getItem(context, next);
	}

	public final Item getPrev() throws IOException {
		return prev == null ? null : ItemManager.getItem(context, prev);
	}

	public final String getId() {
		return FileUtil.getId(context.config, file);
	}

	@Override
	public final String toString() {
		return getClass() + ": " + file;
	}

	public final SortedSet<Item> getChildren() throws IOException {
		return ItemManager.getSortedSet();
	}

	public final String getThumbnail() {
		return content.getThumbnail();
	}

	public String getCssClass() {
		return "content";
	}

	public int getOrder() {
		return 1;
	}

	public final Integer[] getPageCount() {
		return new Integer[0];
	}
}
